package com.scoresystem.web.service;

import java.util.List;

import com.scoresystem.common.CommonValue;
import com.scoresystem.dao.DaoFactory;
import com.scoresystem.dao.IRemarkDao;
import com.scoresystem.dao.IScoreDao;
import com.scoresystem.dao.IStudentDao;
import com.scoresystem.json.JsonContext;
import com.scoresystem.json.JsonState;
import com.scoresystem.model.StuRemark;
import com.scoresystem.model.StuScore;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 用于处理教师成绩录入
 * @author Administrator
 *
 */
public class TeacherService {
	private DaoFactory factory=DaoFactory.getInstance();
	//维护json的状态类
	private JsonState state=new JsonState();
	//根据不同的json状态选择不同的处理方法
	private JsonContext context=new JsonContext(state);
	/**
	 * 查看学生列表
	 * @param stuclass
	 * @param coursename
	 * @return
	 */
	public JSONObject checkoutStuScores(String stuclass, String coursename) {
		IScoreDao scdao = null;
		IStudentDao stdao = factory.createStudentDao();
		List<StuScore> list = stdao.listbyClass(stuclass);
		try {
			if (list == null) {
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("数据库异常", null, null);
			}
			else{
				for(int i=0;i<list.size();i++){
					scdao=factory.createScoreDao();
					StuScore stu=list.get(i);
					String score=null;
					if((score=scdao.getScorebyClassCourse(stu.getStunum(),
							stuclass,coursename))!=null){
						stu.setScore(Integer.parseInt(score));
					}
				}
				JSONArray jsonArr = JSONArray.fromObject(list);
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("查询成功", null,jsonArr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return new JSONObject();
	}
	/**
	 * 
	 * @param stuclass
	 * @param coursename
	 * @param worknum
	 * @param stunum
	 * @param score
	 * @param stuname
	 * @return
	 */
	public JSONObject checkoutInsertScore(String stuclass, String coursename, String worknum, String stunum,
			String score, String stuname) {
		IScoreDao sdao = factory.createScoreDao();
		IRemarkDao rdao = null;
		try {
			if (sdao.insertScore(stuclass, coursename, worknum, stunum, score, stuname)) {
				state.setState(CommonValue.STATE_SUCCESS);

				rdao = factory.createRemarkDao();
				rdao.add(stunum, worknum, "");

				return context.getObject("录入成功", null, null);
			} else {
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("录入失败", null, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new JSONObject();
	}
	/**
	 * 教师查看成绩反馈
	 * @param worknum
	 * @return
	 */
	public JSONObject checkoutLookOutRemark(String worknum){
		IRemarkDao dao=factory.createRemarkDao();
		List<StuRemark> list=null;
		try{
			list=dao.listAll(worknum);
			if(list==null){
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("查询失败", null, null);
			}
			else{
				JSONArray jsonArr = JSONArray.fromObject(list);
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("查询成功", null, jsonArr);
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return new JSONObject();
	}
	/**
	 * 教师修改成绩
	 * @param stuclass
	 * @param coursename
	 * @param worknum
	 * @param stunum
	 * @param score
	 * @return
	 */
	public JSONObject checkoutUpdate(String stuclass,String coursename,
			String worknum,String stunum,String score){
		IScoreDao dao=factory.createScoreDao();
		try{
			if(dao.modify(stuclass, coursename, worknum, stunum, score)){
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("修改成功", null, null);
			}
			else{
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("修改失败", null, null);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return new JSONObject();
	}
}
